<?php 
namespace app\common\controller;

use app\common\model\coin\CoinUser;
use app\common\model\coin\CoinFinance;
use app\common\model\user\Users;
use app\common\model\user\UserActFinance;

class Finance
{
	/**
	 * 添加财务
	 * @param int $user_id 用户ID
	 * @param int $coin_id 币种ID
	 * @param int $account_style 账户类型1可用余额，2冻结余额
	 * @param double $price 变动金额
	 * @param string $memo 描述
	 */
	public static function addFinance($user_id,$coin_id,$account_style,$price,$memo)
	{
		$coin_user = CoinUser::where(['user_id' => $user_id,'coin_id' => $coin_id])->find();
		$data = [
				'user_id' => $user_id,
				'coin_id' => $coin_id,
				'account_style' => $account_style,
				'price' => $price,
				'avalible_price' => $coin_user['avalible_price'],
				'freezing_price' => $coin_user['freezing_price'],
				'memo' => $memo,
				'create_time' => time(),
				'update_time' => time()
		];
		CoinFinance::create($data);
	}
	
	/**
	 * 添加代理财务
	 * @param unknown $user_id
	 * @param unknown $from_id
	 * @param unknown $price
	 * @param unknown $memo
	 * @param unknown $type
	 * @param number $coin_id
	 */
	public function addActFinance($user_id,$from_id,$price,$memo,$type,$coin_id = 5)
	{
		$user = Users::get($user_id);
		$yprice = $type == 1 ? $user->account : $user->bail;
		$data = [
				'user_id' => $user_id,
				'coin_id' => $coin_id,
				'from_id' => $from_id,
				'type' => $type,
				'price' => $price,
				'yprice' => $yprice,
				'memo' => $memo,
				'create_time' => time(),
				'update_time' => time()
		];
		UserActFinance::create($data);
	}
}